package org.shangrila.app.wfm.mb;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.shangrila.comm.utils.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUploadProcessor implements Serializable {
	private static final Logger log = LoggerFactory
			.getLogger(FileUploadProcessor.class);
	private static final long serialVersionUID = 489545609258183431L;

	private List<String> fileList = null;
	private String selectedFile;
	private List<FileDownloadObject> selectedFiles;
	private String selectedFilePath=" ";
	private StreamedContent file;
	
	public FileUploadProcessor() {

	}
	public void showDownloadFiles() {

		selectedFiles = new ArrayList<FileDownloadObject>();
		
		for(String filename:fileList){	
			
			InputStream stream = null;
			try {
				stream = new FileInputStream(selectedFilePath + filename);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}							
			file = new DefaultStreamedContent(stream, "application/octet-stream",
					filename); // application/octet-stream 代表任意二进制流
	
			selectedFiles.add(new FileDownloadObject(filename,file));
	
		}
	}

	public static void saveInputStreamAsFile(InputStream is, String fileName) {
		try {
			FileUtil.createFile(fileName);
			OutputStream os = new FileOutputStream(fileName);
			int byteCount = 0;
			byte[] buffer = new byte[1024];
			while ((byteCount = is.read(buffer)) != -1) {
				os.write(buffer, 0, byteCount);
			}
			is.close();
			os.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	// downloadFile selected-row：操作系统目录
	public void downloadFileSelected(String osFilePath) {		
		String filename = selectedFilePath + selectedFile;
		log.info(osFilePath + " is selected. download filename is:"+filename);
		InputStream stream = null;
		try {
			stream = new FileInputStream(filename);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		file = new DefaultStreamedContent(stream, "application/octet-stream",
				selectedFile); // application/octet-stream 代表任意二进制流
		//return null;
	}

	// downloadFile selected-row：web工程目录 "/resources/images/shangrila.png"
	public String downloadFileWeb(String webFilePath) {
		ServletContext servletContext = (ServletContext) FacesContext
				.getCurrentInstance().getExternalContext().getContext();
		InputStream stream = servletContext
				.getResourceAsStream(webFilePath); 
		file = new DefaultStreamedContent(stream, "image/png",
				"downloaded_file_name.png");
		return null;
	}
			
}
